package org.ms.mconf.sdk.model;

import com.alibaba.fastjson.JSON;
import lombok.*;
import org.micro.URL;
import org.ms.mconf.sdk.Constants;
import org.ms.mconf.sdk.service.type.ActionType;

import java.io.Serializable;
import java.util.List;

/**
 * The Data Entry Model
 *
 * @author lry
 */
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class DataConfig implements Serializable {

    /**
     * The config key
     */
    private String key;

    /**
     * The config status(0=disable, 1=enable)
     */
    private int status;

    /**
     * The config value
     */
    private Object value;

    /**
     * The config introduce
     */
    private String introduce;

    /**
     * The creator
     */
    private String createBy;

    /**
     * The create time
     */
    private long createAt;

    /**
     * The changer
     */
    private String updateBy;

    /**
     * The change time
     */
    private long updateAt;

    /**
     * The build data value
     *
     * @param dataConfig {@link DataConfig}
     * @return build result key
     */
    public static String buildDataValue(DataConfig dataConfig) {
        return JSON.toJSONString(dataConfig);
    }

    /**
     * The parse data field value
     *
     * @param value data value
     * @return {@link DataConfig}
     */
    public static DataConfig parseDataValue(String value) {
        return JSON.parseObject(value, DataConfig.class);
    }

    /**
     * The encode channel value
     * <p>
     * format-> encode(key):JSON
     *
     * @param actionType  {@link ActionType}
     * @param dataConfigs {@link DataConfig}
     * @return channel message
     */
    public static String buildMessage(ActionType actionType, List<DataConfig> dataConfigs) {
        return URL.encode(actionType.toString()) + Constants.ACTION_SEQ + JSON.toJSONString(dataConfigs);
    }

    /**
     * The parse message
     *
     * @param message message json string
     * @return {@link List<DataConfig>}
     */
    public static List<DataConfig> parseMessage(String message) {
        String jsonMessage = message.substring(message.indexOf(Constants.ACTION_SEQ) + 1);
        return JSON.parseArray(jsonMessage, DataConfig.class);
    }

    /**
     * The parse ActionType
     *
     * @param message channel message
     * @return {@link ActionType}
     */
    public static ActionType parseActionType(String message) {
        if (message.indexOf(Constants.ACTION_SEQ) < 1) {
            throw new IllegalArgumentException("The illegal action type: " + message);
        }

        return ActionType.valueOf(URL.decode(message.substring(0, message.indexOf(Constants.ACTION_SEQ))));
    }

    /**
     * The check not blank
     *
     * @param dataConfig {@link DataConfig}
     */
    public static void checkNotBlank(DataConfig dataConfig) {
        if (dataConfig == null
                || dataConfig.getKey() == null || dataConfig.getKey().length() == 0
                || dataConfig.getValue() == null || String.valueOf(dataConfig.getValue()).length() == 0
                || dataConfig.getIntroduce() == null || dataConfig.getIntroduce().length() == 0
                || dataConfig.getCreateAt() <= 0 || dataConfig.getUpdateAt() <= 0
                || dataConfig.getCreateBy() == null || dataConfig.getCreateBy().length() == 0
                || dataConfig.getUpdateBy() == null || dataConfig.getUpdateBy().length() == 0) {
            throw new NullPointerException();
        }
    }

}